package crawler;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author sunzhihao2
 * @date 15:45 2018/6/7
 */
public class CrawlerForIndustryStatus {

    private static Logger logger = LoggerFactory.getLogger(CrawlerForIndustryStatus.class);
    //创建默认客户端
    private static CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    public static String find(String stockCode){
        //创建
        HttpGet httpGet = new HttpGet("http://stockpage.10jqka.com.cn/"+stockCode+"/field/");
        //向对方服务器发送Post请求
        try {

            httpGet.addHeader("UserAgent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            httpGet.addHeader("Accept-Encoding","gzip, deflate");
            httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.9");
            httpGet.addHeader("Connection","keep-alive");
            CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
            String result = "";

            //如果模拟登录成功
            if(httpResponse.getStatusLine().getStatusCode() == 200) {
                String entity = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
                httpGet.abort();//释放资源
                return entity;
                //return JsonUtil.convertToStockVos(entity);
            }

            httpGet.abort();//释放资源
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpGet.abort();//释放资源
        }
        return null;
    }

}
